import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:meco/common/index.dart';
import 'dart:io';

import 'index.dart';

class SetupPage extends GetView<SetupController> {
  const SetupPage({super.key});

  // 主视图
  Widget _buildView() {
    return Stack(
      children: [
        Positioned.fill(child: Image.asset(Images.startBg, fit: BoxFit.cover)),
        // 内容
        SafeArea(
          child: Obx(() {
            final user = controller.currentUser.value;

            if (user == null) {
              return Center(
                child: CircularProgressIndicator(color: Colors.white),
              );
            }

            return Padding(
              padding: EdgeInsets.symmetric(horizontal: 20.w),
              child: Column(
                children: [
                  SizedBox(height: 20.h),

                  // 设置列表
                  Expanded(
                    child: ListView(
                      children: [
                        // 用户头像项
                        _buildAvatarItem(user),

                        // 其他设置项
                        _buildSettingsItem(
                          title: 'Privacy Policy',
                          onTap: controller.privacyPolicy,
                        ),
                        _buildSettingsItem(
                          title: 'Terms Of Service',
                          onTap: controller.termsOfService,
                        ),
                        _buildSettingsItem(
                          title: 'Eula',
                          onTap: controller.eula,
                        ),
                        _buildSettingsItem(
                          title: 'Delete account',
                          onTap: controller.deleteAccount,
                        ),
                      ],
                    ),
                  ),

                  // 退出登录按钮
                  Container(
                    width: double.infinity,
                    margin: EdgeInsets.only(bottom: 30.h),
                    child: ElevatedButton(
                      onPressed: controller.logout,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Color(0xFF6B9CE5),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(30.r),
                        ),
                        padding: EdgeInsets.symmetric(vertical: 15.h),
                      ),
                      child: Text(
                        'Log out',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            );
          }),
        ),
      ],
    );
  }

  // 构建头像列表项
  Widget _buildAvatarItem(User user) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: () => controller.changeAvatar(),
        splashColor: Colors.white.withOpacity(0.1),
        highlightColor: Colors.white.withOpacity(0.05),
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(vertical: 15.h),
          child: Row(
            children: [
              // 头像
              Container(
                width: 54.w,
                height: 54.w,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  border: Border.all(color: Colors.white, width: 1.w),
                ),
                child: ClipOval(
                  child:
                      user.avatar != null && user.avatar!.isNotEmpty
                          ? (user.avatar!.startsWith('assets/')
                              ? Image.asset(user.avatar!, fit: BoxFit.cover)
                              : Image.file(
                                File(user.avatar!),
                                fit: BoxFit.cover,
                                errorBuilder: (context, error, stackTrace) {
                                  return Image.asset(
                                    Images.avatar1,
                                    fit: BoxFit.cover,
                                  );
                                },
                              ))
                          : Image.asset(Images.avatar1, fit: BoxFit.cover),
                ),
              ),
              Spacer(),
              Icon(Icons.arrow_forward_ios, color: Colors.white, size: 16.sp),
            ],
          ),
        ),
      ),
    );
  }

  // 构建设置项
  Widget _buildSettingsItem({
    required String title,
    required VoidCallback onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        splashColor: Colors.white.withOpacity(0.1),
        highlightColor: Colors.white.withOpacity(0.05),
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(vertical: 15.h),
          child: Row(
            children: [
              Text(
                title,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
              Spacer(),
              Icon(Icons.arrow_forward_ios, color: Colors.white, size: 16.sp),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SetupController>(
      init: SetupController(),
      id: "setup",
      builder: (_) {
        return Scaffold(
          extendBodyBehindAppBar: true,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            elevation: 0,
            title: Text(
              "set up",
              style: TextStyle(
                color: Colors.white,
                fontSize: 18.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
            centerTitle: true,
            leading: IconButton(
              icon: Icon(IconFontIcons.iconBack, color: Colors.white),
              onPressed: () => Get.back(),
            ),
            systemOverlayStyle: SystemUiOverlayStyle.light,
          ),
          body: _buildView(),
        );
      },
    );
  }
}
